package main

import (
	"fmt"
	"github.com/smartwalle/alipay/v3"
	"net/http"
	"os/exec"
	"strings"
	"time"
)

var (
	// appId
	appId = "2016093000635483"
	// 应用公钥
	aliPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApbLjJQ2nTPWxlez4naeTuLYGp7ZVN4OtuJvYsfMBYa//oyAksisvUoXS80PMI/yqfkaTbEIEJvB7F9mp5ZNL1Ip/VsW+0h+5E1GoaYhhEOhFhmeecvvi53gt/mBl3X/UwnxpsRHC0lVADMfMBKdD+GZxDRxOlD8lefrq8Uc5ah20QvmZS47ycWPx5c5Cz/q+pVXIMBr6SxPqq2ORaYzhWNejEyZobCGlKEht1oag2LAXOLVnNOXmV0ZKMqjkmBm001f+eeac18k7mK5PrBAF0tzRvswOfWaMsDuD0UcTPEv7sEQH2z/mA8CzDzv84iCjTqa/hBWSI60QiQUuim0UdQIDAQAB"
	// 应用私钥
	privateKey = "MIIEowIBAAKCAQEApbLjJQ2nTPWxlez4naeTuLYGp7ZVN4OtuJvYsfMBYa//oyAksisvUoXS80PMI/yqfkaTbEIEJvB7F9mp5ZNL1Ip/VsW+0h+5E1GoaYhhEOhFhmeecvvi53gt/mBl3X/UwnxpsRHC0lVADMfMBKdD+GZxDRxOlD8lefrq8Uc5ah20QvmZS47ycWPx5c5Cz/q+pVXIMBr6SxPqq2ORaYzhWNejEyZobCGlKEht1oag2LAXOLVnNOXmV0ZKMqjkmBm001f+eeac18k7mK5PrBAF0tzRvswOfWaMsDuD0UcTPEv7sEQH2z/mA8CzDzv84iCjTqa/hBWSI60QiQUuim0UdQIDAQABAoIBACWb27h+R9nM7ljEJ43JCltjXVFVrjPifFypat7A9DBS/SfuVMXnzV7ipwW1/rCwWZY9uJpaxYx9CsNYERrdetmr8cklKvGQZ7Z900bYwIdQ4O7nt+wLRe4Ztf7FrHlA2W6nRGn/SQPqe5YrByCLQn8v6Tr7kM5PItdz3z2SV/LAmuMuFyVFTcfhoQv1ph7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("D:\\2345Downloads\\alishaxiang\\appCertPublicKey_2016093000635483.crt")
	client.LoadAliPayPublicCert("D:\\2345Downloads\\alishaxiang\\alipayCertPublicKey_RSA2.crt")
	client.LoadAliPayRootCert("D:\\2345Downloads\\alishaxiang\\alipayRootCert.crt")
}

//App支付
func AppAlipay() {
	pay := alipay.TradeAppPay{}
	// 支付宝回调地址（需要在支付宝后台配置）
	// 支付成功后，支付宝会发送一个POST消息到该地址
	pay.NotifyURL = "http://www.pibigstar/alipay"
	//支付标题
	pay.Subject = "支付宝支付测试"
	//订单号，一个订单号只能支付一次
	pay.OutTradeNo = time.Now().String()
	//销售产品码，与支付宝签约的产品码名称,目前仅支持FAST_INSTANT_TRADE_PAY
	pay.ProductCode = "FAST_INSTANT_TRADE_PAY"
	//金额
	pay.TotalAmount = "6666"

	url, err := client.TradeAppPay(pay)
	if err != nil {
		fmt.Println(err)
	}
	//这个 payURL 即是用于支付的 URL，可将输出的内容复制，到浏览器中访问该 URL 即可打开支付页面。
	fmt.Println(url)
}

//网站扫码支付
func WebPageAlipay() {
	pay := alipay.TradePagePay{}
	// 支付宝回调地址（需要在支付宝后台配置）
	// 支付成功后，支付宝会发送一个POST消息到该地址
	pay.NotifyURL = "http://www.pibigstar/alipay"
	// 支付成功之后，浏览器将会重定向到该 URL
	pay.ReturnURL = "http://localhost:8088/return"
	//支付标题
	pay.Subject = "支付宝支付测试"
	//订单号，一个订单号只能支付一次
	pay.OutTradeNo = time.Now().String()
	//销售产品码，与支付宝签约的产品码名称,目前仅支持FAST_INSTANT_TRADE_PAY
	pay.ProductCode = "FAST_INSTANT_TRADE_PAY"
	//金额
	pay.TotalAmount = "6666"

	url, err := client.TradePagePay(pay)
	if err != nil {
		fmt.Println(err)
	}
	payURL := url.String()
	//这个 payURL 即是用于支付的 URL，可将输出的内容复制，到浏览器中访问该 URL 即可打开支付页面。
	fmt.Println(payURL)

	//打开默认浏览器
	payURL = strings.Replace(payURL, "&", "^&", -1)
	exec.Command("cmd", "/c", "start", payURL).Start()
}

//手机客户端支付
func WapAlipay() {
	pay := alipay.TradeWapPay{}
	// 支付成功之后，支付宝将会重定向到该 URL
	pay.ReturnURL = "http://localhost:8088/return"
	//支付标题
	pay.Subject = "支付宝支付测试"
	//订单号，一个订单号只能支付一次
	pay.OutTradeNo = time.Now().String()
	//商品code
	pay.ProductCode = time.Now().String()
	//金额
	pay.TotalAmount = "0.01"

	url, err := client.TradeWapPay(pay)
	if err != nil {
		fmt.Println(err)
	}
	payURL := url.String()
	//这个 payURL 即是用于支付的 URL，可将输出的内容复制，到浏览器中访问该 URL 即可打开支付页面。
	fmt.Println(payURL)
	//打开默认浏览器
	payURL = strings.Replace(payURL, "&", "^&", -1)
	exec.Command("cmd", "/c", "start", payURL).Start()
}

func main() {
	//生成支付URL
	AppAlipay()
	//支付成功之后的返回URL页面
	http.HandleFunc("/return", func(rep http.ResponseWriter, req *http.Request) {
		req.ParseForm()
		ok, err := client.VerifySign(req.Form)
		if err == nil && ok {
			rep.Write([]byte("支付成功"))
		}
	})
	//支付成功之后的通知页面
	http.HandleFunc("/alipay", func(rep http.ResponseWriter, req *http.Request) {
		var noti, _ = client.GetTradeNotification(req)
		if noti != nil {
			fmt.Println("支付成功")
			//修改订单状态。。。。
		} else {
			fmt.Println("支付失败")
		}
		alipay.AckNotification(rep) // 确认收到通知消息
	})

	fmt.Println("server start....")
	http.ListenAndServe(":8088", nil)
}
